package com.xxyy.mylive.ui.activity;


import android.content.res.Configuration;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.orhanobut.logger.Logger;
import com.xxyy.mylive.R;
import com.xxyy.mylive.tools.UiTool;
import com.xxyy.mylive.ui.BaseActivity;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class VrVideoActivity extends BaseActivity {
    private JCVideoPlayerStandard mPlayer;
    private ImageButton btnBack;
    private SensorManager sensorManager;
    private JCVideoPlayer.JCAutoFullscreenListener sensorEventListener;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_vr_video);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Logger.i("landscape");
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Logger.i("portrait");
        }
    }

    @Override
    protected void initUI() {
        btnBack = (ImageButton) findViewById(R.id.btn_back);
        mPlayer = (JCVideoPlayerStandard) findViewById(R.id.video_view);
    }

    @Override
    protected void setListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        String mPlayURL = getIntent().getStringExtra("play_stream");
        Logger.i("播放rtmp地址：" + mPlayURL);
        mPlayer.setUp(mPlayURL, JCVideoPlayerStandard.CURRENT_STATE_NORMAL, "VR视频");
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();

        Point point = UiTool.getScreenPoint(this);
        int width = point.x;
        int height = point.y;
        Logger.i("屏幕高度为====="+height);
        ViewGroup.LayoutParams params = mPlayer.getLayoutParams();
        params.height= height/3;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mPlayer.startButton.performClick();
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
